<?php
class CommendModel extends Model {
    public function __construct() {
        parent::__construct();
        $this->fields = array('id','content','star','goodsid','orderid','user','date','attr','re_content','is_block');
        $this->tables = array(DB_FREFIX.'commend');
        list(
                $this->R['id'],
                $this->R['goodsid'],
                $this->R['orderid'],
                $this->R['user']
        ) = $this->getRequest()->getParams(
            array(
                //加上一层判断消除notice警告
                isset($_GET['id']) ? $_GET['id'] : null,
                isset($_GET['goodsid']) ? $_GET['goodsid'] : null,
                isset($_GET['orderid']) ? $_GET['orderid'] : null,
                isset($_COOKIE['user']) ?$_COOKIE['user'] : null
            )
        );
    }

    //查询多条
    public function findAll() {
        return parent::select(array(DB_FREFIX.'goods g',DB_FREFIX.'commend c'),array('g.name name,g.nav nav,c.id,c.content,c.star,c.user,c.goodsid,c.attr,c.re_content'),array('where'=>array('g.id=c.goodsid'),'limit'=>$this->limit,'order'=>'c.date DESC'));
    }

    public function findOne() {
        $condition = array('orderid='.$this->R['orderid'].' AND goodsid='.$this->R['goodsid']);
        return parent::select($this->tables,array('star','content','re_content'),array('where'=>$condition,'limit'=>'1'));
    }

    public function findUpdateOne() {
        $condition = array('id='.$this->R['id']);
        return parent::select($this->tables,array('id','user','content','star','attr','re_content','is_block'),array('where'=>$condition));
    }

    public function findMyCommend() {
        return parent::select(array(DB_FREFIX.'goods g',DB_FREFIX.'commend c'),array('g.name name,g.nav nav,g.thumbnail thumbnail,c.id,c.date,c.content,c.star,c.user,c.goodsid,c.attr,c.re_content'),array('where'=>array('g.id=c.goodsid AND c.user='.$this->R['user']),'limit'=>$this->limit,'order'=>'c.date DESC'));
    }

    public function add() {
        $needData = $this->getRequest()->filter($this->fields);
        //默认和特殊字段的系统自动填充和修正
        $needData['user'] = $_COOKIE['user'];
        $needData['date'] = Tool::getDate();
        return parent::add($needData);
    }

    public function update() {
        $condition = array('id='.$this->R['id']);
        $data = $this->getRequest()->filter($this->fields);
        return parent::update($data,$condition);
    }

    //删除
    public function delete() {
        $condition = array('id='.$this->R['id'],'limit'=>1);
        return parent::delete($condition);
    }

    //管理员总记录条数
    public function total() {
        if(isset($_COOKIE['user']) && $_GET['a'] == 'member') {
            return parent::total(array('where'=>array('user='.$this->R['user'])));
        } else {
            return parent::total();
        }
    }

    //ajax获得当前商品的的评论总数
    public function ajaxGetPagenum() {
        echo parent::total(array('where'=>array("goodsid='{$_POST['id']}' AND is_block=0")));
    }

    //ajax获取当前商品的所有评论
    public function ajaxGetCommend() {
        $condition = array("goodsid='{$_POST['id']}' AND is_block=0");
        echo json_encode(parent::select($this->tables,array('star','content','user','date','attr','re_content','is_block'),array('where'=>$condition,'limit'=>$_POST['limit'],'order'=>'date DESC')));
    }



}


?>